﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace ViewMaker.Core.Controls
{
    /// <summary>
    /// データグリッドViewコントロール
    /// </summary>
    public class DataGridViewControl : HeaderViewControl
    {
        public new class Properties : HeaderViewControl.Properties
        {
            public const string SelectedItem = "SelectedItem";
            public const string IsReadOnly = "IsReadOnly";
            public const string DoubleClickCommand = "DoubleClickCommand";
            public const string SelectionChangedCommand = "SelectionChangedCommand";
        }

        /// <summary>
        /// 現在選択しているアイテム（バインドするパス）
        /// </summary>
        [View(ViewControlType.ComboBox)]
        [ViewProperty(ComboBoxViewControl.Properties.ItemsSource, "BindablePathList")]
        [ViewProperty(ComboBoxViewControl.Properties.IsEditable, true)]
        public string SelectedItem { get; set; }

        /// <summary>
        /// 読み取り専用
        /// </summary>
        [DefaultValue(false)]
        public bool IsReadOnly { get; set; }

        /// <summary>
        /// 選択データ変更時に実行するコマンド
        /// </summary>
        [Display(Name = "SelectionChanged", GroupName = CommandGroup)]
        [View(ViewControlType.ComboBox)]
        [ViewProperty(ComboBoxViewControl.Properties.ItemsSource, "Item.CommandList")]
        [ViewProperty(ComboBoxViewControl.Properties.IsEditable, true)]
        public string SelectionChangedCommand { get; set; }

        /// <summary>
        /// ダブりクリックした時に実行するコマンド
        /// </summary>
        [Display(GroupName = CommandGroup)]
        [View(ViewControlType.ComboBox)]
        [ViewProperty(ComboBoxViewControl.Properties.ItemsSource, "Item.CommandList")]
        [ViewProperty(ComboBoxViewControl.Properties.IsEditable, true)]
        public string DoubleClickCommand { get; set; }

        [XmlIgnore]
        [Browsable(false)]
        public override bool CanAddChild { get { return true; } }

        [XmlIgnore]
        [Browsable(false)]
        public override bool CanOverrideWidth { get { return false; } }

        [XmlIgnore]
        [Browsable(false)]
        public override bool CanOverrideHeight { get { return false; } }

        #region ShouldSerialize

        public override bool ShouldSerializeWidth() { return true; }
        public override bool ShouldSerializeHeight() { return true; }

        #endregion

        public DataGridViewControl()
        {
            ControlType = ViewControlType.DataGrid;
            IsPanel = true;
            IsDataGridItem = true;

            HeaderPosition = LayoutHeaderPosition.Hidden;
            //ColumnSpan = 0;

            StreachAlign();
            if (ViewMakerSettings.Instance.DefaultDataGridHeight.HasValue) Height = ViewMakerSettings.Instance.DefaultDataGridHeight;
            if (ViewMakerSettings.Instance.DefaultDataGridWidth.HasValue) Width = ViewMakerSettings.Instance.DefaultDataGridWidth;
        }
        public override List<XamlNode> CreateXaml(XamlNode parent)
        {
            var list = base.CreateXaml(parent);
#if !SILVERLIGHT
            var grid = parent.CreateElement("DataGrid");
#else
            var grid = parent.CreateElement("sdk", "http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk", "DataGrid");
#endif
            if (!AdditionalProperty.ContainsKey("AutoGenerateColumns")) grid.SetAttribute("AutoGenerateColumns", "false");
            if (IsReadOnly) grid.SetAttribute("IsReadOnly", IsReadOnly.ToString());

            SetBindingAttribute(grid, "ItemsSource", Item.BindPath, true);
            if (SelectedItem != null) SetBindingAttribute(grid, "SelectedItem", SelectedItem, false);
#if !SILVERLIGHT
            var columns = grid.AddElement("DataGrid.Columns");
            if (!string.IsNullOrEmpty(SelectionChangedCommand)) AddBehavior(grid, "SelectionChangedBehavior", SelectionChangedCommand);
#else
            var columns = grid.CreateElement("sdk", "http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk", "DataGrid.Columns");
            grid.AppendChild(columns);
            if (!string.IsNullOrEmpty(SelectionChangedCommand)) AddBehavior(grid, "DataGridSelectionChangedBehavior", SelectionChangedCommand);
#endif
            if (!string.IsNullOrEmpty(DoubleClickCommand)) AddBehavior(grid, "DataGridDoubleClickBehavior", DoubleClickCommand);
            foreach (var child in Item.Children)
            {
                foreach (var item in child.CreateXaml(grid))
                {
                    if (child.Element.IsDataGridItem || child.ControlType == ViewControlType.Literal)
                    {
                        columns.AppendChild(item);
                    }
                }
            }
            ApplyBaseProperty(grid);
            list.Add(grid);
            return list;
        }
    }

}
